#include "AL_OD.h"
#include "IO_LINK_Global_Param.h"
#include "Type_Define.h"
#include "DL_Service.h"
#include "DL_ISDU_Handler.h" 
#include "AL_Service.h"
//测试git同步
/********************
     暂时排除在工程外 8.3
     测试git同步8.10
****************/

AL_OD_STATE_TYPE AL_OD_STATE; //AL_OD状态机状态

AL_ISDU_STRUCT_TYPE AL_ISDU_STRUCT; //ISDU通道参数

AL_PAGE_STRUCT_TYPE AL_PAGE_STRUCT; //PAGE通道参数
 

ISDU_Flag_TYPE ISDU_Flag;  //标示使用ISDU通道,根据Device特性得到

uint8_t Current_Port; //当前端口
uint16_t AL_ErrorInfo; //AL层的Error信息
AL_Direction_TYPE AL_Dirction; //AL_Cnf方向
uint8_t * AL_ReadData;//AL_Cnf读取的数据
uint8_t   AL_ReadDataLen; //AL_Cnf读取的数据长度

void Init_AL_OD(void){

    for(uint8_t i=0;i<PORT_NUM;i++){
        AL_OD_STATE=AL_OnReq_Idle_0;
    }
    AL_PAGE_STRUCT_Point=0;
}

void Event_AL_Service(uint8_t Port){
   if(AL_OD_STATE==AL_OnReq_Idle_0){
       AL_OD_STATE=AL_Build_DL_Service_1;
       AL_OD_T1(Port);
   }
   
   
}
void Event_AL_Read(void){
    //已经在Service中检查了当前状态
    uint16_t Index;
    Index=AL_ISDU_STRUCT.Index;
   

    if(Index>=2&&ISDU_Flag==Y_ISDU_Flag){
      AL_OD_STATE=AL_Await_DL_ISDU_cnf_3;
      AL_OD_T6();
      return;
    }

    if(Index<=1){
       AL_OD_STATE=AL_Await_DL_Param_cnf_2;
       AL_OD_T3();
       return;
    }

}


void Event_AL_Write(void){
    //已经在Service中检查了当前状态
    uint16_t Index,Subindex;
    Index=AL_ISDU_STRUCT.Index;
    Subindex=AL_ISDU_STRUCT.SubiIndex;
    if(Index==0&&ISDU_Flag==N_ISDU_Flag&&Subindex==16){
        AL_OD_STATE=AL_Await_DL_Param_cnf_2;
        AL_OD_T18();
        return;
    }
    if(Index==2&&ISDU_Flag==N_ISDU_Flag){
        AL_OD_STATE=AL_Await_DL_Param_cnf_2;
        AL_OD_T5();
        return;
    }
    if(Index>=2&&ISDU_Flag==Y_ISDU_Flag){
        AL_OD_STATE=AL_Await_DL_ISDU_cnf_3;
        AL_OD_T7();
        return;
    }
    if(Index==1){
        AL_OD_STATE=AL_Await_DL_Param_cnf_2;
        AL_OD_T4();
        return;
    }
}
 
void Event_AL_Abort(void){
    if(AL_OD_STATE==AL_Await_DL_Param_cnf_2){
        AL_OD_STATE=AL_Build_AL_cnf_4;
        AL_OD_T9();
    }
}


void Event_DL_ReadParam_Cnf(uint8_t  Data,uint16_t ErrorInfo){
   AL_ErrorInfo=ErrorInfo; //提取DL的ErrorInfo
   if(ErrorInfo==0){
       
           AL_PAGE_STRUCT.Data[AL_PAGE_STRUCT.Data_Pointer]= Data; 
           AL_PAGE_STRUCT.Data_Pointer++;// 更新PAGE数据指针
           //判断读写是否结束
            if(AL_PAGE_STRUCT.Data_Pointer==AL_PAGE_STRUCT.DataLen){ //读写完毕
                AL_OD_STATE=AL_Build_AL_cnf_4;
                
                 AL_OD_T13();             //结束读
            }else{                       //说明有多个Page读写,相当于T10的驱动事件
                AL_OD_T10_R();          //继续读

           }
        
      
   }else{  //如果出错，则直接进入构造AL_Cnf状态
       AL_OD_STATE=AL_Build_AL_cnf_4;
       Event_Service_processed();
   }
 
}




void Event_DL_WriteParam_Cnf(uint16_t ErrorInfo){
    AL_ErrorInfo=ErrorInfo; //提取DL的ErrorInfo
   if(ErrorInfo==0){
       //判断写循环是否结束
        if(AL_PAGE_STRUCT.Data_Pointer==AL_PAGE_STRUCT.DataLen){ //读写完毕
                AL_OD_STATE=AL_Build_AL_cnf_4;
                
                 AL_OD_T14();      //结束写
            }else{                       //说明有多个Page读写,相当于T10的驱动事件
                AL_OD_T10_W();  //继续用PAGE写

           }
 
   }else{  //如果出错，则直接进入构造AL_Cnf状态
       AL_OD_STATE=AL_Build_AL_cnf_4;
       Event_Service_processed();
   }
}

//Data为已经从ISDUBufferIn中提取的有效数据数据，提取的数据存储在AL_ReadData中
void Event_DL_ISDUTransport_Cnf(uint8_t * Data,uint8_t DataLen, uint16_t ErrorInfo){
    if(AL_Dirction==AL_R){
        AL_ReadData=Data;
        AL_ReadDataLen=DataLen;
        AL_ErrorInfo=ErrorInfo;

    }else{

        AL_ErrorInfo=ErrorInfo;
    }
}

 

void Event_Service_processed(void){
    if(AL_OD_STATE==AL_Build_AL_cnf_4){
        AL_OD_T16();
    }
}


void AL_OD_T1(uint8_t Port){
   Current_Port=Port;
   return; //此时又回到Service_AL_Read_Write
   
}

//从AL_PAGE_STRUCT中提取参数，进行DL_ReadParam,T3读取第一个
void AL_OD_T3(void){
   
    uint8_t   Address=AL_PAGE_STRUCT.DirectAddress;
    Service_DL_ReadParam(Current_Port,Address);
     
    
}

void AL_OD_T4(void){
   uint8_t Address=AL_PAGE_STRUCT.DirectAddress;
   uint8_t Data=AL_PAGE_STRUCT.Data[AL_PAGE_STRUCT.Data_Pointer];
   AL_PAGE_STRUCT.Data_Pointer++;
   Service_DL_WriteParam(Current_Port,Address,Data);
   
  
}


//  与T18一样 
void AL_OD_T5(void){
   uint8_t Address=0x0F;
   uint8_t Data=*AL_ISDU_STRUCT.Data; //当Index==2时候，Data存储在ISDU通道
   if(DL_MH_CurrentState[Current_Port]==MH_Startup_2){  //如果是STARTUP状态，
        Service_DL_Write(Current_Port,Address,Data);  //ParameterCommand应保证只用在Startup状态
  
   }else{
       Service_DL_WriteParam(Current_Port,Address,Data);
   }
    AL_PAGE_STRUCT.Data_Pointer++;
}


void AL_OD_T6(void){
    
    ISDU_ValueList_Typedef Valuelist;
    Valuelist.Subindex=Direction_Read;
    Valuelist.Index=AL_ISDU_STRUCT.Index;
    Valuelist.Subindex=AL_ISDU_STRUCT.Subindex;
    Valuelist.OctectString=NULL;
    Valuelist.OctectStringLen=0;
    Service_DL_ISDUTransport(Current_Port,Valuelist);
   
}



void AL_OD_T7(void){
     ISDU_ValueList_Typedef Valuelist;
    Valuelist.Subindex=Direction_Write;
    Valuelist.Index=AL_ISDU_STRUCT.Index;
    Valuelist.Subindex=AL_ISDU_STRUCT.Subindex;
    Valuelist.OctectString=AL_ISDU_STRUCT.Data;
    Valuelist.OctectStringLen=AL_ISDU_STRUCT.DataLen;
    Service_DL_ISDUTransport(Current_Port,Valuelist);

}

 

//void AL_OD_T8() //其功能在AL_Service相关函数中已经实现

void AL_OD_T9(void){
    AL_ReadData=NULL;
    AL_ReadDataLen=0;
    AL_ErrorInfo=0x5700; ErrorType= M_ISDU_ILLEGAL;
}

//T10拆分成读和写两个函数
void AL_OD_T10_R(uint8_t Data){
      AL_PAGE_STRUCT.DirectAddress++; 
      uint8_t   Address=AL_PAGE_STRUCT.DirectAddress;
    Service_DL_ReadParam(Current_Port,Address);
}


void AL_OD_T10_W(void){
    uint8_t Address=AL_PAGE_STRUCT.DirectAddress;
   uint8_t Data=AL_PAGE_STRUCT.Data[AL_PAGE_STRUCT.Data_Pointer];
   AL_PAGE_STRUCT.Data_Pointer++;
   Service_DL_WriteParam(Current_Port,Address,Data);
   
}


//void AL_OD_T12() //其功能在AL_Service相关函数中已经实现

//准备AL_Cnf，要准备好的资料有：读写方向，端口，数据（Read）,数据长度，错误信息 
void AL_OD_T13(void){
    /***
     * 读写方向，在Service_AL_Read中
     * 端口==Current_Port 
     * 本函数准备数据
     * 错误信息在Event_DL_Read(Write)
     * */
    AL_ReadData=AL_PAGE_STRUCT.Data;
    AL_ReadDataLen=AL_PAGE_STRUCT.DataLen;
    
    //AL_Dirction=Direction_Read;
    
    Event_Service_processed();

}

//准备AL_Cnf，要准备好的资料有：读写方向，端口，错误误信息 
void AL_OD_T14(void){
    /***
     * 读写方向，在Service_AL_Read中
     * 端口==Current_Port 
     * 本函数准备数据
     * 错误信息在Event_DL_Read(Write)
     * */
     Event_Service_processed();

}


////准备AL_Cnf，要准备好的资料有：读写方向，端口，数据（Read）,数据长度，错误信息 
void AL_OD_T15(void){
    //所需工作已经在Event_DL_ISDUTransport_Cnf中完成
}


void AL_OD_T16(void){
    if(AL_Dirction==AL_R){
        Service_AL_Read_Cnf(Current_Port,AL_ReadData,AL_ReadDataLen,AL_ErrorInfo);
    }
    else{
        Service_AL_Write_Cnf(Current_Port,AL_ErrorInfo);
    }
}
void AL_OD_T18(void){
   uint8_t Address=0x0F;
   uint8_t Data=*AL_PAGE_STRUCT.Data;
   if(DL_MH_CurrentState[Current_Port]==MH_Startup_2){  //如果是STARTUP状态，
        Service_DL_Write(Current_Port,Address,Data);  //ParameterCommand应保证只用在Startup状态
  
   }else{
       Service_DL_WriteParam(Current_Port,Address,Data);
   }
  AL_PAGE_STRUCT.Data_Pointer++;
}

